package de.studiorutton.gerengdic;

import de.studiorutton.access.FileAccess;
import de.studiorutton.access.FileAccessException;
import de.studiorutton.gerengdic.dictionary.DictionaryActivity;
import de.studiorutton.gerengdic.indexeddictionary.IndexedDictionaryActivity;
import de.studiorutton.gerengdic.library.LibraryDatabase;
import de.studiorutton.gerengdic.update.UpdateDatabaseListActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;

public class LearningWordsActivity extends Activity {
	private static final String TAG = "learningwords";
	public static Boolean ENABLE_RESTART = false;
	
	private AboutDialogs ad;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Log.d( TAG, "LearningWordsActivity:onCreate" );
		try {
			FileAccess.checkExternalStorageExists();
		} catch (FileAccessException e) {
			showErrorDialog(e.getLocalizedMessage(), true );
			return;
		}
		
		getWindow().setFormat( PixelFormat.RGBA_8888);
		
		if ( Prefs.getInstance().versionChanged() ) {
			showDialog( AboutDialogs.ABOUT_DIALOG_CHANGELOG );
			return;
		}
		
		if (!checkDatabase())
			return;
		
		ENABLE_RESTART = true;
		gotoDefaultDictionary();
	}

	@Override
	protected void onStart() {
		super.onStart();
		// Log.d(TAG, "LearningWordsActivity:onStart");
	}

	@Override
	protected void onResume() {
		super.onResume();
		// Log.d(TAG, "LearningWordsActivity:onResume");
	}

	@Override
	protected void onPause() {
		super.onPause();
		// Log.d(TAG, "LearningWordsActivity:onPause");
	}

	@Override
	protected void onStop() {
		super.onStop();
		// Log.d(TAG, "LearningWordsActivity:onStop");
	}

	@Override
	public void onRestart() {
		super.onRestart();
		// Log.d( TAG, "LearningWordsActivity:onRestart" );
		gotoDefaultDictionary();
	}
	
	@Override
	public void onNewIntent(Intent i) {
		if (!checkDatabase())
			return;
		
		ENABLE_RESTART = true;
		gotoDefaultDictionary();
	}
	
	@Override
	public Dialog onCreateDialog(int id) {
		ad = new AboutDialogs(this);
		if ( id == AboutDialogs.ABOUT_DIALOG_CHANGELOG) {
			return ad.changelogDialog(changelogListener);
		} else
			return null;
	}

	public DialogInterface.OnClickListener changelogListener = new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			Intent i = new Intent( getApplicationContext(), LearningWordsActivity.class );
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}
	};	

	public static final int DATABASE_UPDATE = 10;
	public static final int DATABASE_FRESH_LOAD = 11;

	public static final int DICTIONARY_RUN = 20;
	public static final int HISTORY = 30;
	public static final int DATABASE_UPDATE_CANCELED = 1;
	public static final int DATABASE_UPDATE_OK = 2;
	public static final int DICTIONARY_RETURN = 3;

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Log.d(TAG, "Requestcode = " + requestCode + " Resultcode = "
		// + resultCode);
		if (requestCode == DATABASE_UPDATE) {
			if (resultCode == DATABASE_UPDATE_OK) {
				ENABLE_RESTART = true;
				gotoDefaultDictionary();
			} else
				finish();

		} else if (requestCode == DATABASE_FRESH_LOAD) {
			if (resultCode == DATABASE_UPDATE_OK) {
				ENABLE_RESTART = true;
				gotoDefaultDictionary();
			} else
				finish();
		} else if (requestCode == DICTIONARY_RUN) {
			// if ( resultCode == DICTIONARY_RETURN ) {
			// finish();
		}

	}

	public boolean checkDatabase() {
		try {
			FileAccess.checkExistsAndReadable(LibraryDatabase.get()
					.getFileLocation(LibraryDatabase.DE_EN_DICT));
		} catch (FileAccessException fe) {
			Intent i = new Intent(getApplicationContext(),
					UpdateDatabaseListActivity.class);
			startActivityForResult(i, DATABASE_FRESH_LOAD);
			return false;
		}
		return true;
	}

	public void gotoDefaultDictionary() {

		// Log.d(TAG,
		// "LearningWordsActivity:gotoDefaultDictionary: ENABLE_RESTART = "
		// + (ENABLE_RESTART ? "true" : "false"));

		if (ENABLE_RESTART) {
			Intent i;
			if (Prefs.getInstance().getDictionaryMode().equals("1")) {
				i = new Intent(getApplicationContext(),
						IndexedDictionaryActivity.class);
			} else
				i = new Intent(getApplicationContext(),
						DictionaryActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			finish();
		} else
			finish();
	}

	public void showErrorDialog(String msg, final boolean finish) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getResources().getString(R.string.error_occurred))
				.setMessage(msg)
				.setCancelable(false)
				.setPositiveButton(getResources().getString(R.string.okay),
						new android.content.DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								if ( finish )
									finish();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}
}